package com.woniu.teachmall04.controller;

import com.woniu.teachmall04.entity.Admin;

import com.woniu.teachmall04.entity.Users;
import com.woniu.teachmall04.service.UserService;
import com.woniu.teachmall04.service.admin.AdminService;
import jakarta.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    UserService userService;
    @GetMapping("")  //  /admin
    public String admin(HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser.getAccount().equals("admin")){
            return "admin";
        }

        return "redirect:/";
    }
    @GetMapping("/user/list")
    public String userList(HttpSession session, Model model) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(!loginUser.getAccount().equals("admin")){
            return "login";
        }
        //获取所有的用户数据
        List<Users> userlist = adminService.getAllUsers();
        model.addAttribute("userlist", userlist);
        return "admin_list";
    }

    //针对普通用户的操作
    //冻结
    @GetMapping("/user/freeze")
    public String freeze(@RequestParam Integer id, HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser==null || !loginUser.getAccount().equals("admin")){
            return "login";
        }
        //冻结用户
        userService.freeze(id);
        return "redirect:/admin/user/list";
    }
    //解冻
    @GetMapping("/user/unfreeze")
    public String unfreeze(@RequestParam Integer id, HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser==null || !loginUser.getAccount().equals("admin")){
            return "login";
        }
        //冻结用户
        userService.unfreeze(id);
        return "redirect:/admin/user/list";
    }
    //加入黑名单
    @GetMapping("user/blacklist/add")
    public String blacklistadd(@RequestParam Integer id, HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser==null || !loginUser.getAccount().equals("admin")){
            return "login";
        }
        //加入黑名单用户
        userService.blacklist(id);
        return "redirect:/admin/user/list";
    }
    //解除黑名单
    @GetMapping("user/blacklist/remove")
    public String blacklistremove(@RequestParam Integer id, HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser==null || !loginUser.getAccount().equals("admin")){
            return "login";
        }
        //加入黑名单用户
        userService.unblacklist(id);
        return "redirect:/admin/user/list";
    }
    //删除
    @GetMapping("user/delete")
    public String deleteUser(@RequestParam Integer id, HttpSession session) {
        Users loginUser = (Users) session.getAttribute("loginUser");
        if(loginUser==null || !loginUser.getAccount().equals("admin")){
            return "login";
        }
        //删除用户
        userService.delete(id);
        return "redirect:/admin/user/list";
    }

}
